//
//  LoginViewController.m
//  YiXiProject
//
//  Created by iMac on 2019/7/16.
//  Copyright © 2019 iMac. All rights reserved.
//

#import "LoginViewController.h"

#import "LoginView.h"                 /** 登陆view */
#import "HomeMarryViewController.h"   /** 首页 */


@interface LoginViewController () <LoginDelegate>

@property (nonatomic,strong) LoginView *loginV; // view属性


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //注册键盘弹出通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self creatLoginView];
    
}

// 初始化页面view
-(void)creatLoginView
{
    self.loginV = [[LoginView alloc]initWithFrame:self.view.bounds];
    //self.loginV.backgroundColor = GreenColor;
    [self.view addSubview:self.loginV];
    self.loginV.delegate = self;
    
    // 渐变器
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSMutableArray arrayWithObjects:(id)ColorAlphe(250, 94, 99, 1).CGColor, (id)ColorAlphe(227, 40, 118, 1).CGColor, nil];
    
    // 渐变
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(0, 1);
    
    [self.view.layer insertSublayer:gradient atIndex:0];
}

// 点击登陆按钮走这里，把用户登陆信息通过接口传给后台
-(void)clickLoginButton:(NSInteger)tag
{
    switch (tag)
    {
        case 100:
            NSLog(@"登陆");
            [self clickDL];
            break;
            
        case 101:
            [self.navigationController pushViewController:[NSClassFromString(@"RegistViewController") new] animated:YES];
            break;
            
        case 102:
            [self dismissViewControllerAnimated:YES completion:nil];
            break;
            
        default:
            break;
    }
}
// 登陆
-(void)clickDL
{
    NSString *name = self.loginV.emailText.text;
    NSString *pass = self.loginV.password.text;
    
    [HttpDatas requestLoginUser_type:@"1" email:name password:pass success:^(id  _Nonnull json) {
        
        if([json isKindOfClass:[NSDictionary class]])
        {
            if([json[@"status"]integerValue] == 200)
            {
                [YXUserdefaults PutDefaults:@"email" Value:name];
                [YXUserdefaults PutDefaults:@"password" Value:pass];
                [YXUserdefaults PutDefaults:@"isLogin" Value:@"YES"];
                [YXUserdefaults PutDefaults:@"token" Value:json[@"data"][@"token"]];
                [YXUserdefaults PutDefaults:@"user_id" Value:json[@"data"][@"id"]];
                
                HomeMarryViewController *homeVC = [[HomeMarryViewController alloc]init];
                UINavigationController *navRoot = [[UINavigationController alloc]initWithRootViewController:homeVC];
                KeyWindow.rootViewController = navRoot;
                
                [SVProgressHUD showSuccessWithStatus:@"登陆成功"];
                [SVProgressHUD dismissWithDelay:SVPROGRESSHUDTIME];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:json[@"message"]];
                [SVProgressHUD dismissWithDelay:SVPROGRESSHUDTIME];
            }
        }
        
    } failure:^(NSError * _Nonnull err) {
        
        [SVProgressHUD showErrorWithStatus:@"登陆失败"];
        [SVProgressHUD dismissWithDelay:SVPROGRESSHUDTIME];
        
    }];
}

//键盘弹出后将视图向上移动
-(void)keyboardWillShow:(NSNotification *)note
{
    NSDictionary *info = [note userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    
    //目标视图UITextField
    CGRect frame = self.view.frame;
    int y = frame.origin.y + frame.size.height - (self.view.frame.size.height - keyboardSize.height/2);
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    if(y > 0)
    {
        self.view.frame = CGRectMake(0, -y, self.view.frame.size.width, self.view.frame.size.height);
    }
    [UIView commitAnimations];
}

//键盘隐藏后将视图恢复到原始状态
-(void)keyboardWillHide:(NSNotification *)note
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self cancelEdid];
}

// 取消编辑
-(void)cancelEdid
{
    [self.loginV.emailText resignFirstResponder];
    [self.loginV.password resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
